/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conf2.model;

import conf2.model.EActor.Side;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Administrator
 */
public class Deck
{

    private Set<ECard> unplayedCards;
    private Set<ECard> playedCards;
    private ECard currentCard = null;
    private ECard putAside = null;
    private final Side side;

    public Deck(Side side)
    {
        assert side != null;
        this.side = side;
        reset();
    }

    public void reset()
    {
        ECard[] cards = (side == Side.DARK ? ECard.DARK_CARDS : ECard.LIGHT_CARDS);

        unplayedCards = EnumSet.copyOf(Arrays.asList(cards));
        playedCards = EnumSet.noneOf(ECard.class);
        currentCard = null;
        putAside = null;
    }

    public void pickUp(ECard card)
    {
        assert unplayedCards.contains(card);
        assert currentCard == null;
        currentCard = card;
        unplayedCards.remove(card);
    }

    public void putBack()
    {
        assert currentCard != null;
        assert !unplayedCards.contains(currentCard);
        assert !playedCards.contains(currentCard);
        unplayedCards.add(currentCard);
        if (putAside != null) {
            unplayedCards.add(putAside);
        }
    }

    public void discard()
    {
        assert currentCard != null;
        playedCards.add(currentCard);
        currentCard = null;
        if (putAside != null) {
            unplayedCards.add(putAside);
        }
    }

    public void putAsideForNow()
    {
        assert currentCard != null;
        assert putAside == null;
        putAside = currentCard;
        currentCard = null;
    }

    public int unplayedCount()
    {
        return unplayedCards.size();
    }

    public int playedCount()
    {
        return playedCards.size();
    }

    public List<ECard> getUnplayedAsSortedList()
    {
        List<ECard> list = new LinkedList<ECard>(unplayedCards);
        Collections.sort(list, ECard.getComparator());
        return list;
    }

    public List<ECard> getPlayedAsSortedList()
    {
        List<ECard> list = new LinkedList<ECard>(playedCards);
        Collections.sort(list, ECard.getComparator());
        return list;
    }

    public ECard getCurrent()
    {
        return currentCard;
    }
}
